package com.fl.nettydemo.c4;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.logging.Logger;

/**
 * 服务器连接客户端
 */
@Slf4j
public class client {
    public static void main(String[] args) {
        try {
            String info = "";
            SocketChannel channel = SocketChannel.open();
            channel.connect(new InetSocketAddress("localhost", 9999));
            log.info("wait....");

            while (!info.equals("quit")) {
                Scanner sc = new Scanner(System.in);
                info = sc.nextLine();
                if (info.equals("quit")) {
                    channel.close();
                    log.info("客户端关闭:");
                } else {
                    int write = channel.write(Charset.defaultCharset().encode(info));
                    log.info("客户端发送消息:{},{}", info, LocalDateTime.now());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
